package welsir.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import welsir.entity.dos.Square;
import welsir.entity.dos.SquareClass;
import welsir.entity.dos.User;
import welsir.entity.vo.ResultMessage;
import welsir.entity.vo.SquarePageVO;
import welsir.mapper.SquareMapper;
import welsir.mapper.UserMapper;
import welsir.security.annotations.Authentication;
import welsir.security.enums.UserEnums;
import welsir.service.ISquareClassService;
import welsir.service.ISquareCommentService;
import welsir.service.ISquareService;
import welsir.service.IUserService;
import welsir.util.ResultUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Genius
 * @date 2023/03/08 20:25
 **/
@RestController
@RequestMapping("/square")
public class SquareController {

    @Resource
    private SquareMapper squareMapper;

    @Autowired
    private ISquareService squareService;

    @Resource
    private IUserService userService;

    @Autowired
    private ISquareClassService squareClassService;

    @Autowired
    private ISquareCommentService squareCommentService;

    //获取全部圈子

    @Authentication(role= UserEnums.ADMIN)
    @ApiOperation(value = "根据别名获取全部圈子(分页)")
    @ApiImplicitParam(name = "otherName",value = "otherName",required = true)
    @GetMapping("/getAllSquare/{otherName}/{page}/{limit}")
    public ResultMessage<SquarePageVO> getAllSquareByName(
            @PathVariable("otherName") String otherName,
            @PathVariable("page") Integer page,
            @PathVariable("limit") Integer limit
    ) {

        return ResultUtil.data(squareService.VoList(otherName, page, limit));
    }

    @Authentication(role= UserEnums.ADMIN)
    @ApiOperation(value = "根据id删除圈子")
    @ApiImplicitParam(name = "id",value = "文章id",required = true)
    @GetMapping("/deleteSquareById/{id}")
    public ResultMessage<Boolean> deleteSquareById(
            @PathVariable("id") Integer id
    ) {
        //根据id删除
        QueryWrapper<Square> wrapper= new QueryWrapper<Square>();
        wrapper.eq("id",id);
        squareMapper.delete(wrapper);
        return ResultUtil.data(this.squareService.removeById(id));
    }

    @Authentication(role= UserEnums.ADMIN)
    @ApiOperation(value = "根据id修改圈子内容")
    @ApiImplicitParam(name = "id",value = "文章id",required = true)
    @GetMapping("/ChangeSquareById/{id}/{content}")
    public ResultMessage<Integer> ChangeSquareById(
            @PathVariable("id") Integer id,
            @PathVariable("content") String content
    ) {

        Square square = new Square();
        square.setContent(content);
        QueryWrapper<Square> wrapper= new QueryWrapper<Square>();
        wrapper.eq("id",id);

        return ResultUtil.data(squareMapper.update(square,wrapper));
    }

    @Authentication(role= UserEnums.ADMIN)
    @ApiOperation(value = "获取全部圈子用户")
    @GetMapping("/GetAllSquareUser")
    public ResultMessage<List<User>> GetAllSquareUser() {

        return ResultUtil.data(userService.list());
    }
}
